package gold.digger;

import gold.utils.InputUtil;

/**
 * Created by fanzhenyu02 on 2021/12/10.
 * common problem solver template.
 */
public class LC985 {
    public long startExecuteTime = System.currentTimeMillis();


    class Solution {
        public int[] sumEvenAfterQueries(int[] A, int[][] queries) {
            int[] result = new int[queries.length];
            //偶数标志位数组
            boolean[] flag = new boolean[A.length];
            int sum = 0;
            //首先遍历求得所以偶数和
            for (int i = 0; i < A.length; ++i) {
                if (A[i] % 2 == 0) {
                    flag[i] = true;
                    sum += A[i];
                }
            }
            for (int i = 0; i < queries.length; ++i) {
                int val = queries[i][0];
                int index = queries[i][1];
                //该位置原本是偶数
                if (flag[index]) {
                    //加上之后还是偶数
                    if ((A[index] + val) % 2 == 0) {
                        sum = sum + val;
                    } else {//加上之后变成了奇数,移除该元素，标志为false
                        sum = sum - A[index];
                        flag[index] = false;
                    }
                } else {//该位置原本是奇数
                    //相加变为偶数，标志变为true
                    if ((A[index] + val) % 2 == 0) {
                        sum = sum + A[index] + val;
                        flag[index] = true;
                    }
                    //还是奇数则不考虑
                }
                //该位置+val
                A[index] += val;
                //加入当前sum
                result[i] = sum;
            }
            return result;
        }
    }

    public void run() {
        Solution solution = new Solution();
        int[] arr = InputUtil.toIntegerArray("[1,2,3]");
        System.out.println(solution.toString());
    }

    public static void main(String[] args) throws Exception {
        LC985 an = new LC985();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
